﻿using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
using UnityEditor.SceneManagement;

namespace Turbo.SceneStreamer
{
    public class SceneBlockTestCaseEditor
    {
        public const string WRITE_MODEL_PATH = @"Assets/SceneStreamer/_Example/Resources/Model/";
        public const string WRITE_MESH_PATH = @"Assets/SceneStreamer/_Example/Resources/Model/Mesh/";
        public const string WRITE_MATERAIL_PATH = @"Assets/SceneStreamer/_Example/Resources/Materials/";
        public const string WRITE_PREFAB_PATH = @"Assets/SceneStreamer/_Example/Resources/Prefabs/";
        
        private static string SceneBlockResourcePath = "Assets/SceneStreamer/_Example/Resources/Scene_Block/";
        private static string SceneTerrainMatPath = SceneBlockResourcePath + "Materials/Terrain.mat";
        private static string ScenePath = "Assets/SceneStreamer/_Example/Scene_Block/";
        private static string MainSceneName = "Example_World";
        private static string SubSceneName = "Example_SubScene";
        private static string OringinPrefabName = "lod_cube";
        private static string ProxyPrefabName = "hlod_cube";

        [MenuItem("测试用例资源生成/1-生成原始场景")]
        private static void GenerateBigWorldSubScene()
        {
            //先生成子场景
            string worldscene_path = ScenePath + MainSceneName + ".unity";
            Scene scene = EditorSceneManager.OpenScene(worldscene_path);
            WorldController wc = Object.FindObjectOfType<WorldController>();
            Vector2 sceneIndex = wc.SubSceneMaxIndex;

            GenerateAllSubScene();

            AssetDatabase.Refresh();
        }

        [MenuItem("测试用例资源生成/2-将生成原始场景转换成StreamScene")]
        private static void GenerateBigWorldStreamScene()
        {
            string worldscene_path = ScenePath + MainSceneName + ".unity";
            EditorSceneManager.OpenScene(worldscene_path);
            WorldController wc = Object.FindObjectOfType<WorldController>();

            Vector2 sceneIndex = wc.SubSceneMaxIndex;
            int sceneSizeXZ = (int)wc.SceneSizeXZ;
            int sceneSizeY = (int)wc.SceneSizeY;
            bool useInstancing = wc.UseInstancing;
            int instancingMinCount = wc.InstancingMinCount;
            
            for (int i = 1; i <= sceneIndex.x; i++)
            {
                for (int j = 1; j <= sceneIndex.y; j++)
                {
                    string scenePath = ScenePath + MainSceneName + "_SubScene_" + i + j + ".unity";
                    EditorSceneManager.OpenScene(scenePath);
                    SceneStreamer sceneStreamer =  GameObject.FindObjectOfType<SceneStreamer>();
                    // SceneStreamer_Editor.GenerateStreamingScene(sceneStreamer, sceneSizeXZ, sceneSizeY, useInstancing, instancingMinCount,Vector3.one);
                }
            }

            AssetDatabase.Refresh();
        }

        //[MenuItem("Tools/以当前场景为世界根生成子场景")]
        public static void GenerateAllSubScene()
        {
            Scene scene = SceneManager.GetActiveScene();
            string scenePath = SceneManager.GetActiveScene().path;
            string sceneDirectory = System.IO.Path.GetDirectoryName(scenePath);
            string subSceneName = scene.name + AssetsPath.SubSceneSuffix;

            //当前为主场景，主场景获取
            WorldController wc = Object.FindObjectOfType<WorldController>();
            if (wc == null)
            {
                UnityEngine.Debug.LogError("未找到 WorldController，确认打开场景为大世界主场景");
                return;
            }

            int subSceneLW = (int)wc.SceneSizeXZ;
            int subSceneH = (int)wc.SceneSizeY;
            Vector2 subSceneMinIndex = wc.SubSceneMinIndex;
            Vector2 subSceneMaxIndex = wc.SubSceneMaxIndex;

            GenerateAllSubScene(sceneDirectory, subSceneName, subSceneLW,subSceneH, subSceneMinIndex, subSceneMaxIndex);
        }
        
        private static void GenerateAllSubScene(string scenedir, string subSceneName,int subSceneLW,int subSceneH, Vector2 subSceneMinIndex, Vector2 subSceneMaxIndex)
        {
            string subscene_template = "Assets/SceneStreamer/_Example/Scene_Block/Template/_SubScene_Template.unity";

            string subscene_pre = scenedir + "/" + subSceneName;
            string subscene_end = ".unity";

            for (int x = (int) subSceneMinIndex.x; x <= (int) subSceneMaxIndex.x; x++)
            {
                for (int y = (int) subSceneMinIndex.y; y <= (int) subSceneMaxIndex.y; y++)
                {
                    //通过模板场景拷贝子场景
                    EditorSceneManager.OpenScene(subscene_template);
                    string subscene_path = subscene_pre + x + y + subscene_end;
                    EditorSceneManager.SaveScene(SceneManager.GetActiveScene(), subscene_path, true);
                    EditorSceneManager.OpenScene(subscene_path);

                    //子场景设置position和index
                    SceneStreamer sceneStreamer = Object.FindObjectOfType<SceneStreamer>();
                    if (sceneStreamer == null)
                    {
                        UnityEngine.Debug.LogError("场景（" + x + y + "）未找到 SceneStreamer 节点");
                        return;
                    }

                    sceneStreamer.Index = new Vector3(x, y, 0);
                    // sceneStreamer.m_SceneSize = new Vector3(subSceneLW, subSceneH, subSceneLW);
                    sceneStreamer.transform.position = WorldController.GetSubScenePositionWS(sceneStreamer.Index, subSceneLW);
                    AssetDatabase.SaveAssets();
                    EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
                }
            }
        }
    }
}